package com.example.renshiguanli.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.renshiguanli.config.PaginationConstant;
import com.example.renshiguanli.mapper.EmpMapper;
import com.example.renshiguanli.mapper.EmpSalaryMapper;
import com.example.renshiguanli.mapper.SalaryMapper;
import com.example.renshiguanli.pojo.Emp;
import com.example.renshiguanli.pojo.Empsalary;
import com.example.renshiguanli.pojo.Salary;
import com.example.renshiguanli.service.EmpSalaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class EmpSalaryController {
    @Autowired
    private EmpSalaryMapper empSalaryMapper;
    @Autowired
    private EmpSalaryService empSalaryService;
    @Autowired
    private SalaryMapper salaryMapper;
    @Autowired
    private EmpMapper empMapper;
    @GetMapping("/salarys")
    public String findAllSalary(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, Model model){
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum= PaginationConstant.CURRENT_NUM;
        }
        PageHelper.startPage(pageNum,PaginationConstant.PAGE_SIZE);
        List<Empsalary> all = empSalaryService.findAll();
        model.addAttribute("salary",all);
        PageInfo<Empsalary> pageInfo=new PageInfo<>(all);
        model.addAttribute("pageInfo",pageInfo);
        return "salary/salary";
    }
    // 来到添加页面
    @GetMapping("/salary")
    public String toAddPage(Model model) {
        // 来到添加页面
        List<Emp> emps =empMapper.getAllEmps();
        model.addAttribute("emps",emps);

        List<Salary> salarys = salaryMapper.findAllSalary();;
        model.addAttribute("salarys",salarys);

        return "salary/add";
    }

    // 添加
    @PostMapping("/salary")
    public String addEmpSalary(Empsalary empsalary) {
        // 保存员工
        empSalaryMapper.addEmpSalary(empsalary);
        // 来到列表页面
        // redirect: 表示重定向到一个地址 /代表当前项目路径
        // forward: 表示转发到一个地址
        return "redirect:/salarys";
    }
    // 来到修改页面
    @GetMapping("/salary/{id}")
    public String toUpdatePage(@PathVariable("id") Integer id, Model model) {
        Empsalary empsalary = empSalaryMapper.findById(id);
        model.addAttribute("empsalary",empsalary);

        List<Emp> emps = empMapper.getAllEmps();
        model.addAttribute("emps",emps);

        List<Salary> salarys = salaryMapper.findAllSalary();
        model.addAttribute("salarys",salarys);

        // 回到修改页面（add是一个修改添加二合一的页面）
        return "salary/add";
    }

    // 修改，需要提交id
    @PutMapping("/salary")
    public String updateEmpsalary(Empsalary empsalary) {
        empSalaryMapper.update(empsalary);
        return "redirect:/salarys";
    }
//    删除
    @DeleteMapping("/salary/{id}")
    public String delete(@PathVariable Integer id){
        empSalaryMapper.deleteEmpSalary(id);
        return "redirect:/salarys";
    }
//    查找信息
    @PostMapping("/salsearch")//提交表单+传回前端的映射
    public String queryEmpsalary(@RequestParam String name, Model model,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum){
        PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);
        //查找数据
        List<Empsalary> empsalarys = empSalaryService.query(name);

        PageInfo<Empsalary> pageInfo=new PageInfo<>(empsalarys);

        model.addAttribute("pageInfo",pageInfo);
        return "salary/salary";
    }
//    根据id查询账单信息
    @GetMapping("/salaryaccount/{id}")
    public String showSalaryAccount(@PathVariable("id") Integer id, Model model) {
        Salary salary = salaryMapper.findById(id);
        model.addAttribute("salary",salary);
        return "salary/salaryaccount";
    }

}
